package cn.aifeix.pet.service.impl;

import cn.aifeix.basic.config.BussinessException;
import cn.aifeix.basic.util.BaiduAuditUtils;
import cn.aifeix.pet.domain.Pet;
import cn.aifeix.pet.domain.PetDetail;
import cn.aifeix.pet.domain.PetOnlineAuditLog;
import cn.aifeix.pet.mapper.PetMapper;
import cn.aifeix.pet.mapper.PetOnlineAuditLogMapper;
import cn.aifeix.pet.service.IPetDetailService;
import cn.aifeix.pet.service.IPetService;
import cn.aifeix.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aifeix
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private IPetDetailService petDetailService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = null;
        if (pet.getDetail() == null){
            petDetail = new PetDetail();
        }else {
            petDetail =  pet.getDetail();
        }
        petDetail.setPetId(pet.getId());
        petDetailService.add(petDetail);

    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        // 先过滤点上架状态的数据 把数据转成map k 主键 v对象
        Map<Long, Pet> petMaps = petMapper.findAll().stream().filter(pet -> pet.getState() == 0)
                .collect(Collectors.toMap(Pet::getId, pet -> pet));
        Date date = new Date();
        for (Long id : ids) {
            Pet pet = petMaps.get(id);
            StringBuilder sb = new StringBuilder();
            if (null != pet){
                if (!BaiduAuditUtils.TextCensor(pet.getName())){
                    sb.append("宠物上架百度AI审核名称失败，宠物名称为：" + pet.getName() + ";");
                }
                if (pet.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                    }
                }
                if (sb.length() > 0){
                    PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                    petOnlineAuditLog.setAuditTime(date);
                    petOnlineAuditLog.setPetId(id);
                    petOnlineAuditLog.setNote(sb.toString());
                    petOnlineAuditLog.setState(0);
                    petOnlineAuditLogMapper.add(petOnlineAuditLog);
                    throw new BussinessException("所选上架:"+pet.getName()+"-含违规内容，请检查后重新上架");
                }
                pet.setState(1L);
                pet.setOnsaletime(date);
                petMapper.update(pet);
                // 成功则添加到日志表
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setAuditTime(date);
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setNote("审核通过");
                petOnlineAuditLog.setState(1);
                petOnlineAuditLogMapper.add(petOnlineAuditLog);
            }
        }

    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        // 不管之前是啥状态，统一批量下架处理
        petMapper.batchOffsale(ids);
    }

    @Override
    public Pet getPetDetail(Long id) {
        return petMapper.getPetDetail(id);
    }

}
